// pages/tool/business-card/add-card/add-card.js
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		data: null,
		id: null,
		addimg: '',
		addlogo: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			modify: options.modify,
			data: options.modify == 1 ? JSON.parse(options.item) : null,
		});
		let logo = ''
		if (this.data.data) {
			console.log(this.data.data)
			this.setData({
				addimg: this.data.data.qr,
				addlogo: this.data.data.logo
			})
		}
	},


	uploadImage: function (e) {
		let n = e.currentTarget.dataset.type
		var that = this;
		wx.chooseImage({
			count: 1, //最多可以选择的图片总数
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				console.log(res)
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths;
				//启动上传等待中...
				wx.showLoading({
					title: '上传中',
				})
					if (tempFilePaths.length === 0) {
						wx.showToast({
							title: '上传失败',
							icon: 'none',
							mask: true,
							duration: 1000
						})
					} else {
						if (n == 'logo') {
							let addlogos =tempFilePaths[0]
							if (addlogos) {
								return app.func.uploadPromise("/image/upload", addlogos, 'file', ).then(function ([code, res]) {
									if (code == 3102 || code == 3103) {
										return app.func.toastPromise('图片内容违规').then((resolve, reject) => {
											reject();
										});
									} else if(code==200){
										console.log(res.data)
										that.setData({
											addlogo:res.data
										})
									
									}
								});
							} 
						}
						if (n == 'Wechat') {
						
							let addimgs =tempFilePaths[0];
							if (addimgs) {
								return app.func.uploadPromise("/image/upload", addimgs, 'file', ).then(function ([code, res]) {
									if (code == 3102 || code == 3103) {
										return app.func.toastPromise('图片内容违规').then((resolve, reject) => {
											reject();
										});
									} else if(code==200){
										that.setData({
											addimg:res.data
										})
									
									}
								});
							} 
						}

					}
					wx.hideLoading({
						success: (res) => {},
					})
				
			}

		});

	
	},


	formsubmit(e) {
		let data = {
			mobile: e.detail.value.mobile,
			company: e.detail.value.company,
			name: e.detail.value.name,
			job: e.detail.value.job,
			email: e.detail.value.email,
			address: e.detail.value.address,
			business: e.detail.value.business,
			intro: e.detail.value.intro,
			wechat: e.detail.value.wechat,
			qr: this.data.addimg,
			logo: this.data.addlogo,
		}
		if (this.data.modify == 1) {
			this.edit(data, this.data.data.id);
		} else {
			this.create(data);
		}

	},
	create(data) {
		app.func.postPromise('/westorecard/create', data)
			.then(([code, res]) => {
				if (code == 200) {
					app.func.toastPromise('创建成功')
						.then(() => {
							wx.navigateBack({
								delta: 1
							})
						})
				} else {
					console.log(res)
					app.func.toastPromise(res.message, 'none', 2000)
				}
			})
	},
	edit(data, id) {
		app.func.postPromise(`/westorecard/modify/${id}`, data)
			.then(([code, res]) => {
				if (code == 200) {
					app.func.toastPromise('修改成功')
						.then(() => {
							wx.navigateBack({
								delta: 1
							})
						})
				} else {
					app.func.toastPromise(res.message, 'none', 2000)
				}
			})
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})